package hr.controller;

import hr.entity.DO.EmployeeProbationReviewDO;
import hr.service.ProbationreviewService;
import hr.utils.CommonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


@Tag(name = "导出Excel管理")
@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ProbationreviewService probationreviewService;

    /**
     * 导出excel 试用期
     *
     * @param response
     * @throws IOException
     * @author 柳继纪
     * @since 2023年12月19日00:23:38
     */
    @Operation(summary = "导出试用期Excel")
    @PostMapping("/exportToProbationReviewExcel")
    public void exportToExcel(
            // @RequestBody EmployeeProbationVo employeeProbationVo,
            HttpServletResponse response) throws IOException {
        List<EmployeeProbationReviewDO> dataList = probationreviewService.getProbationReviewExcel();
        dataList.forEach(e->{
            e.setJoinDateStr(CommonUtils.convertTimestampToYMDAndTime(e.getJoinDate()));
        });

        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("试用期审查表");

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 设置表头样式
        CellStyle headerCellStyle = workbook.createCellStyle();
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 12);
        headerCellStyle.setFont(headerFont);

        // 创建表头并设置样式
        String[] columns = {"员工ID", "试用期", "姓名", "岗位", "地址", "联系方式", "入职时间"};
        for (int i = 0; i < columns.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(columns[i]);
            cell.setCellStyle(headerCellStyle);
        }

        int rowNum = 1;// 从下一行开始写入数据
        for (EmployeeProbationReviewDO data : dataList) {
            Row row = sheet.createRow(rowNum++);
            // 填充数据
            row.createCell(0).setCellValue(data.getEmployeeId());
            row.createCell(1).setCellValue(data.getState());
            row.createCell(2).setCellValue(data.getName());
            row.createCell(3).setCellValue(data.getPositionName());
            row.createCell(4).setCellValue(data.getAddress());
            row.createCell(5).setCellValue(data.getPhone());
            row.createCell(6).setCellValue(data.getJoinDateStr());
        }

        // 调整列宽度
        for (int i = 0; i < dataList.size(); i++) {
            sheet.autoSizeColumn(i); // 对每一列进行宽度自适应调整
        }


        // 设置响应头，告诉浏览器这是一个Excel文档
        response.setContentType("application/vnd.ms-excel");
        // 设置文件名
        response.setHeader("Content-Disposition", "attachment;fileName="
                + URLEncoder.encode("试用期审查表.xls", StandardCharsets.UTF_8));

        ServletOutputStream outputStream = response.getOutputStream();
        // 将表格流写会response
        workbook.write(outputStream);
        outputStream.flush();
        workbook.close();
    }
}
